def IP (n):
    for i in range(2, int (n**0.5)+1):
        if n%i==0:
            return False
    return True

N=int(input())
F=N//1000
G=(N//100)%10
S=(N//10)%10
D=N%10

PM=0

if IP (F) and PM<F: PM=F
if IP (G) and PM<G: PM=G
if IP (S) and PM<S: PM=S
if IP (D) and PM<D: PM=D

FG=F*10+G
if IP (FG) and PM<FG: PM=FG
FS=F*10+S
if IP (FS) and PM<FS: PM=FS
FD=F*10+D
if IP (FD) and PM<FD: PM=FD
GS=G*10+S
if IP (GS) and PM<GS: PM=GS
GD=G*10+D
if IP (GD) and PM<GD: PM=GD
SD=S*10+D
if IP (SD) and PM<SD: PM=SD
FGS=F*100+G*10+S
if IP (FGS) and PM<FGS: PM=FGS
GSD=G*100+S*10+D
if IP (GSD) and PM<GSD: PM=GSD

if IP (N) and PM<N: PM=N

if N<10000 and N>999:
 print(PM)
else:
 print("0")

